#!/bin/bash
#
# USAGE: 
#	report [i12sFolder] "dorisSteps" 
# Example:
#  report "interfero;coherence"

function create(){
generateRandomString
curSetFile=${randomString}
settings save ${curSetFile}

local rFolder iFolder rSteps

if [ $# -eq 2 ]; then
  rFolder="./report"
  iFolder=${1}
  rSteps=${2}
elif [ $# -eq 1 ]; then
  if [ -d "${1}" ]; then 
    rFolder="./report"
    iFolder=${1}
    rSteps="subtrrefpha;coherence"
  else
    rFolder=${runFolder}/report
    iFolder=${i12sFolder}
    rSteps=${1}
  fi
elif [ $# -eq 0 ];then
  rFolder=${runFolder}/report
  iFolder=${i12sFolder}
  rSteps="subtrrefpha;coherence"
else
  echo "Wrong number of options. Try: ? report"  
  return
fi

rFolder=`canonicalPath ${rFolder}`
iFolder=`canonicalPath ${iFolder}`
echo Input Parameters reportFolder=${rFolder} i12sFolder=${iFolder} reportSteps=${rSteps}

curFolder=${PWD}
mkdir -p ${rFolder}
rFile=`canonicalPath ${rFolder}/index.html`

writeHTMLHeader > ${rFile}
for folder in `ls ${iFolder}`
do
  [ -f "${iFolder}/${folder}/settings.set" ] || continue
  cd ${iFolder}/${folder}
  settings load
  raster_format=png
  echo "<H1><a href=\"javascript:toggle('${folder}');\">${folder}</a></H1>" >> ${rFile}
  echo "<div id=\"${folder}\" style=\"display: block\">" >> ${rFile}
  echo " <table><tr>" >> ${rFile}
  c=0
  for step in `echo ${rSteps} | tr ';' '\n'`
  do
    echo "  <td align=\"center\">" >> ${rFile}
    handleStep "${step}"
    echo "  </td>" >> ${rFile}
    if [ $(( c % 3 )) -eq 2 ]; then
      echo "  </tr><tr>" >> ${rFile}
    fi
    c=$(( c + 1 ))
  done
  echo "</tr></table>" >> ${rFile}
  echo "</div>" >> ${rFile}
done
writeHTMLFooter >> ${rFile}
cd ${curFolder}

bold "Reloading user settings from temporary backup file."
settings load ${curSetFile}
rm -rf ${curSetFile}
echo "Report is available at: ${rFile}"
}

function handleStep(){
 step="${1}"
 case ${step} in
 *readfiles)
   reportText ${step}
 ;;
 *porbits)
   reportText ${step}
 ;;
 m_simamp*)
   reportPicture ${step}
 ;;
 m_timing)
   reportText ${step}
 ;;
 *ovs*)
   reportPicture ${step}
 ;;
 *crop*)
   reportPicture ${step}
 ;;
 coarseorb)
   reportText ${step}
 ;;    
 *filtazi*)
   reportPicture ${step}
 ;;
 fine)
   reportText ${step}
 ;;
 reltiming)
   reportText ${step}
 ;;
 demassist)
   reportText ${step}
 ;;
 coregpm)
   reportText ${step}
 ;;
 resample*)
   reportPicture ${step}
 ;;
 filtrange*)
   reportPicture ${step}
 ;;
 interfero*)
   reportPicture ${step}
 ;;
 comprefpha*)
   reportPicture ${step}
 ;;
 subtrrefpha*)
   reportPicture ${step}
 ;;
 comprefdem*)
   reportPicture ${step}
 ;;
 subtrrefdem*)
   reportPicture ${step}
 ;;
 coherence*)
   reportPicture ${step}
 ;;
 filtphase*)
   reportPicture ${step}
 ;;
 unwrap*)
   reportPicture ${step}
 ;;
 dinsar*)
   reportPicture ${step}
 ;;
 slant2h*)
   reportPicture ${step}
 ;;
 geocode*)
   reportPicture ${step}
 ;;
 *)
   reportText ${step}
 ;;
 esac
}

function reportText(){
step="${1}"
echo '<textarea readonly="readonly" rows="24" cols="80">' >> "${rFile}"
s result ${step} >> ${rFile}
echo '</textarea>' >>${rFile}
}

function reportPicture(){
 local step m1 m2
 step="${1}"
 #Are pixels already square?
 if [ -n "${slc_az_res}" ]; then 
   if [ $(( $slc_az_res * ${coh_multilook% *} )) -eq $(( $slc_rg_res * ${coh_multilook#* } )) ]; then 
     #echo ${coh_multilook}
     m1=$(( 20 / ${coh_multilook% *} ))
   else
     m1=${slc_az_res}; m2=${slc_rg_res};
   fi
 else
   m1=$(( 20 / ${coh_multilook% *} ))
 fi 
 #m2=$(( ${m1} * ${coh_multilook#* } ))
 [ "${m1}" == "" ] && m1="20" && m2="20"
 imgFile=`raster auto ${step} -M${m1}/${m2} | tail -n1| cut -d":" -f2 | tr -d " "`
 if [ ! -f "${imgFile}" ]; then
   error "Can not find result for step ${step}."
   [ ! -e "${rFolder}/report_result_not_found.png" ] && cp ${ADOREFOLDER}/gui/graphics/report_result_not_found.png "${rFolder}"
   writePicture report_result_not_found.png 300 300 ${step} >> "${rFile}"
 else
   mv ${imgFile} "${rFolder}"
   writePicture ${imgFile} 300 300 ${step} >> "${rFile}"
 fi
}

function writePicture(){
cat << _EOFF
<table>
 <tr><td><b>${4}</b></td></tr>
 <tr><td>
  <img src="${1}" width="${2}" height="${3}" alt="${4}" />
 </td></tr>
</table>
_EOFF
}

function writeHTMLHeader(){
cat <<_EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
 <HEAD>
  <TITLE>ADORE REPORT</TITLE>
  <script language="javascript"> 
  function toggle( id ) {
          var ele = document.getElementById(id);
          if(ele.style.display == "block") {
                  ele.style.display = "none";
          }
          else {
                  ele.style.display = "block";
          }
  } 
  </script>
 </HEAD>
 <BODY>
_EOF
}

function writeHTMLFooter(){
cat <<_EOF
 </BODY>
</HTML>
_EOF
}

create ${@}
